package com.lijianpo.util.extension.datamasking.ser;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.lijianpo.util.extension.datamasking.annotation.Sensitive;

import java.io.IOException;

/**
 * 字符串数组序列化
 * @author zhangyang
 * @date 2020/11/2 16:58
 */
public class ArraySensitiveSerializer extends BaseSensitiveSerializer<String[]> {


    public ArraySensitiveSerializer(Sensitive sensitiveInfo) {
        super(sensitiveInfo.type(), sensitiveInfo.keepChars());
    }

    @Override
    public void serialize(String[] value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        final int len = value.length;
        gen.setCurrentValue(value);
        gen.writeStartArray();
        serializeContents(value, gen, serializers);
        gen.writeEndArray();
    }

    private void serializeContents(String[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        final int len = value.length;
        if (len == 0) {
            return;
        }
        for (int i = 0; i < len; ++i) {
            String str = value[i];
            if (str == null) {
                provider.defaultSerializeNull(gen);
            } else {
                stringSerialize(value[i], gen);
            }
        }
    }


}
